Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Helps minimize the amount of fs
read and write logic, try/catch
logic, writes clean UTF8 json content, and cleans up byte order mark and newline characters to cleanly read and parse json content.
npm install --save file-it
OR
yarn add file-it
import fileIt from "file-it";
OR
const fileIt = require("file-it");
setJsonValue(filename, key, value, [options])
getJsonValue(filename, key)
readJsonArraySync(filename)
readJsonLinesSync(filename)
readContentFile(filename)
readContentFileSync(filename)
readJsonFile(filename, callback)
readJsonFileSync(filename)
appendJsonFileSync(filename, obj, [options])
writeContentFile(filename, content, callback)
writeContentFileSync(filename, content)
writeJsonFile(filename, obj, [options], callback)
writeJsonFileSync(filename, obj, [options])
findSortedJsonElement(filename, attribute, direction)
filename
the full file pathkey
the name of the element in the json filevalue
the value you want to setconst fileIt = require('file-it')
const file = '/tmp/data.json'
fileIt.setJsonValue(file, "hello", "universe", {spaces: 2});
filename
the full file pathkey
the name of the element in the json fileconst fileIt = require('file-it')
const file = '/tmp/data.json'
await fileIt.setJsonValue(file, "hello", "universe", {spaces: 2});
const val = await fileIt.getJsonValue(file, "hello");
console.log("val: ", val); // prints out "universe"
filename
the full file path
throws
If JSON.parse
throws an error, pass this error to the callbackconst fileIt = require('file-it')
const file = '/tmp/jsonArrayFile.json'
fileIt.readJsonArraySync(file, function (err, data) {
if (err) console.error(err)
else console.log(data)
})
filename
the full file path
throws
If JSON.parse
throws an error, pass this error to the callbackconst fileIt = require('file-it')
const file = '/tmp/linesOfJsonData.json'
fileIt.readJsonLinesSync(file, function (err, data) {
if (err) console.error(err)
else console.log(data)
})
filename
the full file pathconst fileIt = require('file-it')
const file = '/tmp/data.json'
fileIt.readContentFile(file, function (err, data) {
if (err) console.error(err)
else console.log(data)
})
filename
the full file pathconst fileIt = require('file-it')
const file = '/tmp/data.json'
console.log(fileIt.readContentFileSync(file))
filename
the full file path
throws
If JSON.parse
throws an error, pass this error to the callbackconst fileIt = require('file-it')
const file = '/tmp/data.json'
fileIt.readJsonFile(file, function (err, data) {
if (err) console.error(err)
else console.log(data)
})
You can also use this method with promises. The readJsonFile
method will return a promise if you do not pass a callback function.
const fileIt = require('file-it')
const file = '/tmp/data.json'
fileIt.readJsonFile(file)
.then(data => console.log(data))
.catch(error => console.error(error))
filename
: the full file pathcontent
: The string object to write
throws
If an error is encountered reading or parsing the file, throw the errorconst fileIt = require('file-it')
const file = '/tmp/data.json'
console.log(fileIt.readJsonFileSync(file))
filename
: the full file pathobj
: The json object to append to the fileoptions
: Pass in any fs.appendFileSync
options or set replacer
for a JSON replacer. Can also pass in spaces
and override EOL
string.const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.appendJsonFileSync(filename, content, function (err) {
if (err) console.error(err)
})
filename
: the full file pathcontent
: The string object to writeconst fileIt = require('file-it')
const file = '/tmp/data.txt'
const content = "hello world"
fileIt.writeContentFile(filename, content, function (err) {
if (err) console.error(err)
})
const fileIt = require('file-it')
const file = '/tmp/data.txt'
const content = "hello world"
fileIt.writeContentFile(filename, content)
filename
: the full file pathobj
: The json object to writeoptions
: Pass in any fs.writeFile
options or set replacer
for a JSON replacer. Can also pass in spaces
and override EOL
string.const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFile(file, obj, function (err) {
if (err) console.error(err)
})
Or use with promises as follows:
const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFile(file, obj)
.then(res => {
console.log('Write complete')
})
.catch(error => console.error(error))
formatting with spaces:
const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFile(file, obj, { spaces: 2 }, function (err) {
if (err) console.error(err)
})
overriding EOL:
const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFile(file, obj, { spaces: 2, EOL: '\r\n' }, function (err) {
if (err) console.error(err)
})
appending to an existing JSON file:
You can use fs.writeFile
option { flag: 'a' }
to achieve this.
const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFile(file, obj, { flag: 'a' }, function (err) {
if (err) console.error(err)
})
filename
: the full file pathobj
: The json object to writeoptions
: Pass in any fs.writeFileSync
options or set replacer
for a JSON replacer. Can also pass in spaces
and override EOL
string.const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFileSync(file, obj)
formatting with spaces:
const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFileSync(file, obj, { spaces: 2 })
overriding EOL:
const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFileSync(file, obj, { spaces: 2, EOL: '\r\n' })
appending to an existing JSON file:
You can use fs.writeFileSync
option { flag: 'a' }
to achieve this.
const fileIt = require('file-it')
const file = '/tmp/data.json'
const obj = { hello: 'World' }
fileIt.writeJsonFileSync(file, obj, { flag: 'a' })
filename
: the full file pathattribute
: the name of the attribute within a json elementdirection
: the sort direction ["asc" | "desc"] - default is descconst fileIt = require('file-it')
const file = '/tmp/data.json'
const topElement = fileIt.findSortedJsonElement(file, "count")
const fileIt = require('file-it')
const file = '/tmp/data.json'
const bottomElement = fileIt.findSortedJsonElement(file, "count", "asc")
FAQs
Easily read/write/update JSON files
The npm package file-it receives a total of 662 weekly downloads. As such, file-it popularity was classified as not popular.
We found that file-it demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 6 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.